package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.base.util.UserContext;
import cn.wolfcode.trip.base.domain.SystemMessage;
import cn.wolfcode.trip.base.query.SystemMessageQuery;
import cn.wolfcode.trip.base.service.ISystemMessageService;
import cn.wolfcode.trip.base.util.JSONResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/systemMessages")
public class SystemMessageController {
    @Autowired
    private ISystemMessageService systemMessageService;
    /**
     * 查询所有游记列表
     * @return
     */
    @GetMapping
    public PageInfo listTravels(SystemMessageQuery qo){
        //传入当前用户
        qo.setPageSize(10);
        qo.setOrderBy("sm.state desc");
        qo.setAuthorId(UserContext.getCurrentUser().getId());
        return systemMessageService.query(qo);
    }
    /**
     * 更改系统消息读取状态
     * @return
     */
    @PutMapping
    public JSONResult changeState(SystemMessageQuery qo) {
        qo.setAuthorId(UserContext.getCurrentUser().getId());
        qo.setState(SystemMessage.STATE_SEEN);
        systemMessageService.changeState(qo);
        return new JSONResult();
    }

    /**
     * 查询系统消息数量
     * @return
     */
    @GetMapping("/{id}")
    public int noSeen(@PathVariable Long id){
        return systemMessageService.queryMessageCount(id,SystemMessage.STATE_NOSEEN);
    }
}
